/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.discoveryservice.config;

import static java.io.File.separator;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Map.Entry;

import org.ourgrid.common.config.Configuration;
import org.ourgrid.common.util.StringUtil;
import org.ourgrid.reqtrace.Req;

import br.edu.ufcg.lsd.commune.container.servicemanager.ServiceManager;
import br.edu.ufcg.lsd.commune.context.ModuleContext;
import br.edu.ufcg.lsd.commune.identification.ServiceID;

public class DiscoveryServiceConfiguration extends Configuration {

	private static final long serialVersionUID = 1L;

	public static final String DISCOVERY_SERVICE = DiscoveryServiceConfiguration.class.getName();
	
	public static final String PREFIX = "ds.";

	public static final String CONF_DIR = findConfDir();

	public static final String PROPERTIES_FILENAME = CONF_DIR + separator + "ds.properties";

	public static final String PROP_DS_NETWORK = PREFIX + "network";
	
	/**
	 * Creates a DiscoveryService configuration, defining the default values and overwriting
	 * it with the values of ds.properties 
	 */
	@Req("REQ010")
	public DiscoveryServiceConfiguration() {
		setProperty( PROP_LOG_PROPERTIES_FILE, "ds.log.properties" );
		loadPropertiesFromFile( PROPERTIES_FILENAME );
	}


	/**
	 * Returns the configuration directory.
	 * 
	 * @return The directory.
	 */
	@Req("REQ010")
	public static String findConfDir() {
		String property = System.getProperty( "OGROOT" );
		return property == null ? "." : property;
	}

	
	public static List<ServiceID> parseNetwork(ServiceManager serviceManager) {
		
		String networkStr = serviceManager.getContainerContext().getProperty(PROP_DS_NETWORK);
		
		if (networkStr == null) {
			return new LinkedList<ServiceID>();
		}
		
		return StringUtil.splitAddresses(networkStr);
	}



	@Override
	protected String getPrefix() {
		return PREFIX;
	}


	@Override
	@Req("REQ010")
	public String getConfDir() {
		return CONF_DIR;
	}


	@Override
	public String getLogPath() {
		String logfile = getProperty( PROP_LOGFILE );
		if ( logfile == null ) {
			logfile = getConfDir() + "/log/ds.log";
		}
		return logfile;
	}


	public static void persistNetwork(List<String> discoveryServicesAddresses, ModuleContext context) throws IOException {
		Properties props = new Properties();
		
		for (Entry<String, String> entry : context.getProperties().entrySet()) {
			props.put(entry.getKey(), entry.getValue());
		}
		
		props.put(PROP_DS_NETWORK, StringUtil.concatAddresses(discoveryServicesAddresses));
		FileOutputStream out = new FileOutputStream(PROPERTIES_FILENAME);
		
		props.store(out, null);
		
		out.flush();
		out.close();
	}



}
